package com.lune.blog.system.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lune.blog.base.domain.LoginUser;
import com.lune.blog.base.domain.system.SysUser;
import com.lune.blog.base.result.CommonResult;
import com.lune.blog.base.result.R;
import com.lune.blog.system.service.MenuService;
import com.lune.blog.system.service.RoleService;
import com.lune.blog.system.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yanjizhw@163.com
 * @date 2021/10/12 11:50
 * @since 1.0.0
 * 描述 用户
 */
@RestController
@RequestMapping("user")
public class UserController{

    @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;

    @Resource
    private MenuService menuService;

    @GetMapping("/info/{username}")
    public R<LoginUser> info(@PathVariable("username") String username) {
        SysUser sysUser = userService.getOne(new QueryWrapper<SysUser>().eq("user_name", username).eq("del_flag", 1));
        if (ObjectUtil.isNull(sysUser)) {
            return R.fail("用户名或密码错误");
        }
        // 角色集合
        Set<String> roles = roleService.getRolePermission(sysUser.getUserId());
        // 权限集合
        Set<String> menus = menuService.getMenuPermission(sysUser.getUserId());
        LoginUser sysUserVo = LoginUser.builder().sysUser(sysUser).roles(roles).permissions(menus).build();
        return R.ok(sysUserVo);
    }

}
